<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12 0012
 * Time: 下午 12:04
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class Module extends BasicAdmin
{
    public $table = 'CategoryModule';

    public function index()
    {
        $this->title = '首页导航';
        $db = Db::name($this->table)->where(['is_deleted' => 1])->order('sort asc,id asc');
        return parent::_list($db, false);
    }

    protected function _index_data_filter(&$data)
    {
        foreach ($data as &$vo) {
            $vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
        }
        $data = ToolsService::arr2table($data);
    }

    /**添加
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**编辑
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

    /**无限极处理
     * @param $vo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            // 读取上级分类
            $where = ['is_deleted' => 1];
            $_cates = (array)Db::name($this->table)->where($where)->order('sort asc,id asc')->select();
            array_unshift($_cates, ['id' => 0, 'pid' => -1, 'name' => '--- 顶级栏目 ---']);
            $cates = ToolsService::arr2table($_cates);
            foreach ($cates as $key => &$cate) {
                if (isset($vo['pid'])) {
                    $path = "-{$vo['pid']}-{$vo['id']}";
                    if ($vo['pid'] !== '' && (stripos("{$cate['path']}-", "{$path}-") !== false || $cate['path'] === $path)) {
                        unset($cates[$key]);
                    }
                }
            }
            $this->assign('cates', $cates);
        }
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("分类禁用成功！", '');
        }
        $this->error("分类禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("分类启用成功！", '');
        }
        $this->error("分类启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $id = $this->request->post('id');
        $id_arr = explode(',', $id);
        foreach ($id_arr as $i) {
            if (in_array($i, [1, 2, 3, 31, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 32, 24, 25, 26, 27, 28, 29, 30, 33, 34, 35, 36, 37, 38, 39])) {
                $this->error('请勿删除默认模块，不需要时请禁用');
            }
        }
        if (DataService::update($this->table)) {
            $this->success("分类删除成功！", '');
        }
        $this->error("分类删除失败，请稍候再试！");
    }
}